using System.Collections.Generic;
using Newtonsoft.Json;
using Voodoo.Sauce.Common.Utils;

namespace Voodoo.Sauce.Core
{
	public static class UserPurchasedProducts
	{
		private const string JsonKey = "UserPurchasedProductsKey";

		public static readonly List<ProductReceivedInfo> All;

		public static List<ProductReceivedInfo> Consumables => Get(PurchaseProductType.Consumable);

		public static List<ProductReceivedInfo> NonConsumables => Get(PurchaseProductType.NonConsumable);

		public static List<ProductReceivedInfo> Subscriptions => Get(PurchaseProductType.Subscription);

		private static List<ProductReceivedInfo> Get(PurchaseProductType type)
		{
			return All.FindAll((ProductReceivedInfo product) => product.ProductType == type);
		}

		static UserPurchasedProducts()
		{
			All = JsonConvert.DeserializeObject<List<ProductReceivedInfo>>(NintendoPlayerPrefs.GetString("UserPurchasedProductsKey")) ?? new List<ProductReceivedInfo>();
		}

		public static void Add(ProductReceivedInfo purchasedProduct)
		{
			if (!All.Contains(purchasedProduct))
			{
				All.Add(purchasedProduct);
				Save();
			}
		}

		private static void Save()
		{
			string value = JsonConvert.SerializeObject(All);
			NintendoPlayerPrefs.SetString("UserPurchasedProductsKey", value);
		}

		public static void ReSendPurchases(PurchaseProductType purchaseType, PurchaseDelegateList purchaseDelegateList)
		{
			foreach (ProductReceivedInfo item in Get(purchaseType))
			{
				PurchaseValidation purchaseValidation = new PurchaseValidation(ValidationStatus.Accepted, item.TransactionID, true, null);
				bool uNITY_IOS = PlatformUtils.UNITY_IOS;
				purchaseDelegateList.OnPurchaseComplete(item, purchaseValidation, uNITY_IOS);
			}
		}

		public static void ReSendPurchases(PurchaseProductType purchaseType, IPurchaseDelegateWithInfo purchaseDelegateWithInfo)
		{
			foreach (ProductReceivedInfo item in Get(purchaseType))
			{
				PurchaseValidation purchaseValidation = new PurchaseValidation(ValidationStatus.Accepted, item.TransactionID, true, null);
				purchaseDelegateWithInfo.OnPurchaseComplete(item, purchaseValidation);
			}
		}
	}
}
